using System.Collections.Generic;
using UnityEngine;

public class SkinCustomisationController : MonoBehaviour
{
	public PersonaModel displayModel;

	private Persona myPersona;

	private const CustomisationConfig.LookSetType type = CustomisationConfig.LookSetType.Skin;

	public List<SkinSelectButton> customisationButtons = new List<SkinSelectButton>();

	private List<CustomisationConfig.Look> looksToManage = new List<CustomisationConfig.Look>();

	public void UpdateWithPersona(Persona persona)
	{
		myPersona = persona;
		InitialiseLooksToManage();
		ApplyInitialLooks();
		InitialiseButtons();
	}

	private void InitialiseLooksToManage()
	{
		looksToManage.Clear();
		CustomisationConfig.LookSet lookSetForType = CustomisationConfig.instance.GetLookSetForType(CustomisationConfig.LookSetType.Skin);
		foreach (CustomisationConfig.Look look in lookSetForType.looks)
		{
			AddLookIfAvailable(look);
		}
	}

	private void AddLookIfAvailable(CustomisationConfig.Look look)
	{
		if (look.IsAvailable(myPersona))
		{
			looksToManage.Add(look);
		}
	}

	private void ApplyInitialLooks()
	{
		CustomisationConfig.Look lookForType = myPersona.GetLookForType(CustomisationConfig.LookSetType.Skin);
		if (lookForType != null)
		{
			lookForType.ApplyLook(displayModel, CustomisationConfig.ShaderUseType.MenuConfigPlayer);
		}
	}

	private void InitialiseButtons()
	{
		for (int i = 0; i < customisationButtons.Count; i++)
		{
			CustomisationConfig.Look look = ((looksToManage.Count <= i) ? null : looksToManage[i]);
			InitialiseButton(customisationButtons[i], look);
		}
	}

	private void InitialiseButton(SkinSelectButton button, CustomisationConfig.Look look)
	{
		button.gameObject.SetActive(look != null);
		if (look != null)
		{
			button.Init(look);
			button.UpdateButtonSelectionHighlight(myPersona.IsLookActive(look));
		}
	}

	public void SelectLook(CustomisationConfig.Look look)
	{
		myPersona.ChangeLook(look);
		look.ApplyLook(displayModel, CustomisationConfig.ShaderUseType.MenuConfigPlayer);
		UpdateButtonHighlights();
	}

	private void UpdateButtonHighlights()
	{
		for (int i = 0; i < customisationButtons.Count; i++)
		{
			UpdateButtonHighlightIfActive(customisationButtons[i], i);
		}
	}

	private void UpdateButtonHighlightIfActive(SkinSelectButton button, int currentButtonIndex)
	{
		if (currentButtonIndex < looksToManage.Count)
		{
			button.UpdateButtonSelectionHighlight(myPersona.IsLookActive(button.myLook));
		}
	}
}
